Welcome back to part two of our introduction to lists.
So in the last session we learned about the general structure of lists and how to create lists.
So now in this session we learn how to select single and multiple element elements from a list and how
to manipulate and change elements of a list.
And we also learn how to slice lists and make some selection of lists.
So what now.
Let's start with our lists which we already know
in our first task is we want to select the very first element of this list and the general rule is that
elements of a list can be assessed.
This sped up records so we take our spot bracket and let's try to get them the very first element of
the list.
So you type in 1 and let's see what we get so we get two.
So two is.
Yeah.
Actually the second element.
So how do we get now our first element.
And yeah the others actually that we have to do type in 0 as index.
And let us see what we get.
So if we're type in 0 to get the first element here it's 1.
And this is neither a mistake or our special last change behavior.
Python Yeah it's a common commonly seen pattern in programming or coding that you have the so-called
zero based indexing.
So the very first element of a sequence has the index Position ZERO.
And then the second tests the index position one the third element has the index to show position 2
and so on.
Now this is a general rule in many programming languages.
So once again it's called zero based indexing.
All right.
So what if now we want to get the fifth element of our list.
So the fifth element is five.
So consequently we have to uh input the index position far.
And now it should return us five.
And this is true.
Alternatively we can also assess the very last element of a sequence by typing the index position minus
one so minus one gets us the very last element of a sequence.
So in this case it's five and the second very last element of our list can be assessed by the index
position minus two.
And consequently the first element of our list can also be indexed by minus five
so that two ways to two index our lists.
So that's the zero based indexing starting from zero.
So our element 1 is has an exposition 0 in exposition 1 in exposition 2 index position 4 sorry in exposition
3 and index position 4 and the alternative phase would be to have index position minus 1 index position
minus 2 index position minus 3 index position minus 4 and index position minus 5.
So and this holds true fire almost every kind of sequence.
So not only false but also for two tuples which we see later off on umpire race and so on.
So yeah this is a very important and basic basic rule here.
How to to assess elements of a sequence and what you can also do you can do it the other way around.
So you can foreign give given element let's say 5 percent can give you the index position so and um
you need to the method index and let's type in 5.
So we would expect B five is at index position far so we would expect here to return for and that's
correct.
Lists are generally mutable.
So what does it mean mutable.
So you can change elements of Alice.
And how do we do this.
So we first have to select our element here we want to select our very the very first element that index
position zero and then we assign with the equals sign a new value here it's hundred.
So let's see what we get.
And here you can see now our lists.
The first element is a hundred and not one.
So we simply changed the first element at an exposure Position ZERO and we can also change the very
last element in our sequence.
So l at an exposition minus one equals hundred one.
Let's see if it works.
And yes it works.
Now what if we want to select many elements of a list and we can do this with a so-called slicing by
using a colon inside the spout brackets.
So in this example here we have a slice from the second element that in exposition one.
And by definition this element is including so from first position unto the fourth index position but
here excluding so let's see what happens.
So we have two three and four.
So we are slicing the list from index position 1 including until index position 4 excluding.
So let's check again here.
Let's see at the list.
So here is an exposition one including until an exposition for but excluding so we get to three and
four
and we can also slice from index position 1 to index position five.
So here in our list there is no index position 5.
So by default we get all elements from index position 1 until the very end.
So let's check and we get to 3 4 and yeah here 101.
And um the alternative way to slice until the very last element including the last element is to to
have just one colon and then nothing
so this means we are slicing from one until the very last element including the last element.
And we can also slice from our very first element.
So at an exposition 0 2 let's say in exposition 3 excluding let's see what we get.
So one hundred two and three as we have including the very first element and the index position 3 so
0 1 2 3.
But excluding so one hundred two and three and the alternative way to do this.
So slicing from the very first element to let's say the element that anyway in exposition 3 is to have
just hear nothing.
Colon 3.
So this is the same actually.
So on one more example.
So let's slice from index position 1 including 2.
Next position 3 excluding and we get 2 and 3 and we can also select all elements by just having a colon
and on the left side of the colon on the right side of the colon nothing gives less than the whole list.
All right so let's enlarge our list with some more elements.
So from 1 to 10 and what we can also do we can choose every second element of our list and we can do
this by having uh two colons.
Um this gives us here every second element beginning from the first including so we have fear starting
from the very first element every second element.
So 1 3 5 7 and 9 and we can also select every third element beginning from the first element by having
double colon and then every three
so we are starting from the first element and then selecting every third element.
So we have 1 4 7 and 10 and we can also start with.
For example the second element in exposition 1 and then select every second element by defining here.
Okay let's start from the second element add an exposition 1 but please only give me every second element.
So we get 2 4 6 8 and 10.
So we are starting from the second element.
And then slicing every second element.
So we have to for 6 8 and 10.
So let's start again with ETL.
That's our list.
So from one to 10.
And we can also assign many new value values to many elements.
So for example if I want to assign to the element that takes position for 50 and to the element that
an exposition 5 51 and to the element that an exposition 652 I can do this by slicing L and assigning
the new values which are here in the list
so as you can see here we have at an exposition 5 or so 0 1 2 3 4 we are 50 50 one that the next position
5 and 50 two at an exposition 6.
All right.
So we cannot do as these so-called broadcasting.
So if we want to have that an exposition for five and six if we want to have 50 then our code as we
have it here is not working.
So we get an error message.
And alternatively how we have to assign two uh in exposition 450 550 and 650.
So we have to do it manually
and then we have 50 at an exposition for five and six alternatively we could have created a list of
three times 50.
So that's that's not out of fear and what we can also do we can delete an element at a certain index
processional so let's say we want to delete them the very first element that in the next position zero.
We can do this and as you can see here the first element so one is removed.
Another thing I want to show you is a nested list or lists of lists.
So the last session we learned that we can actually put any data type into a list and this holds also
true for our list itself so you can just put one on many lists into a list.
And for example we have here a list of let's say football players.
So in each football player has a name and if for example as the height one point eighty six metres and
has a rate so eighty two kilograms and we have four players on our list then for each player we have
one list with the name length and the height so we can create the list and then we have the list with.
So it's a nested list with the four elements and each element is the list itself and we can also print
the type and the length of the list.
Let's do this so we have of course this is a list and it has four elements and we can also slice or
select elements off our list so we can select the very first element at an exposition zero.
So I would expect to get them.
Yeah.
And here
exactly.
So we get we have high end video and use one point it's six meters tall and yes eighty two kilograms.
You can also get the very last player at an exposition three or minus one as you like.
So we get Mike so now and but we can also do we can select the height off from the very first player.
So let's say Andy and we can do that by.
First of all it's select Yeah.
So now we have a sub selection we fear the subtlest and then we want to select the second element of
the index position one we can do this.
And so we have selected the height of India.
So it's one point eighty six meters.
And what we can also do we can create a list containing only all four names of our players.
So we create a list called names.
We open this up brackets and then we say OK the first element should be the very first player.
And the first element.
So at the next position zero is the name here it's Andy.
And then give us the name of the second player give us the name of the third player.
And the name of the fourth player
so now we have a list containing only the names of our players.
So we have a list of four strings.
And that's another thing I want to show you here.
Let's create a list X with four elements.
And then we create another variable Y.
And we assign x.
So here y plus reference to the same list.
And now if you change the very first element of X let's say x that in exposition 0 is 100
so now the list of X is one hundred two three four.
And now if you are checking again I y y.
So why has also changed.
So why is this the case.
Well and what Tyson does internally here it creates a list object and references it makes a reference
to rival x.
And here we assigning y to X so we are creating a reference from X to the same list object.
So we only have one list object and we have two references so one reference to X and one reference to
Y.
And now if you change our object by changing the first element of X Y references to the changed object
so maybe it was our intention that also y changed.
But what if it's not our intention that Y changes if exchanges and what we can do here is that we say
so let's start again here with X equals one two three far and then we create a copy here of the list
by saying okay please create me a copy of X
and to sign it to y so now Y is referencing to a copy of x.
So not to to here the.
The object itself.
So now we created two objects so we have one object the original one that is referencing two X or Xs
referencing to the object and we have a second object which is actually the same elements but only Y
is referencing to the new through the copy a new object.
And if you change it here X now
so why does not change and that's another way to do this.
So let's start again.
So we we are creating an object.
One two three four and assigning X
so we can also create a copy of X by saying okay please give me all elements of x.
So by writing it here and in this manner we are also creating a copy.
So why is it now referencing to our copy so we have two objects object one referencing two X and object
two we are only at Y it's referencing to it.
Now if you change X
also there's no change with Y.
So we have two alternative solutions where we prevent that.
Also Y is changed if we we change x.
All right.
So at the beginning of the session yeah I said that the elements of sequences can be assessed with spec
brackets and this not only holds far lists but also for another sequences or other data types which
are sequences like strings.
So if you create a string Doc dog is a sequence of three characters d o g.
And we can also select here the first element at an exposition 0.
So it's an d and we can also select the very last element G.
And before we set that list mutable so we can change elements in our list.
And that's the difference to strings so strings are immutable.
If we want to assign a new value to let's say in exposition one let's say a stack instead of dark and
then Python gives us an error message.
So this is an example of that.
String objects or as a sequence that is are immutable.
All right.
So now we have finished our session here and I hope you have a better understanding of lists and in
our next session we are going to the next the data type which is lossless sequence tuples so happy to
see you in the next session by.